/**
 * @Author: lusx
 * @Date:   21-03-2019 04:08:15
 * @Project: Govern督办
 * @Last modified by:   lusx
 * @Last modified time: 28-03-2019 05:35:58
 */
import React from "react";
import { Tools } from "weaver-mobile";
import { RouteLayout } from "weaver-mobile-page";
const getLabel = Tools.getLabel;
const ButtonGroup = RouteLayout.ButtonGroup;

//路由层组件
//注: 所有用到该组件的页面,关闭该组件的提交方法名一定要是 handleSubmit
//const HocRouteLayout = WrappedComponent =>
function HocRouteLayout(WrappedComponent) {
  return class extends React.Component {
    render() {
      const { history, hasBtns = true } = this.props;
      let store = {};
      Object.keys(this.props).forEach(key => {
        if (key.includes("Store")) {
          store = this.props[key].actionSheetStore;
        }
      });
      const { handleSubmit = () => {} ,clearData} = store;
      const btns = hasBtns
        ? [
            {
              key: "clear",
              text: `${getLabel(201, "取消")}`,
              onClick: () => history.go(-1)
            },
            {
              key: "submit",
              text: `${getLabel(615, "提交")}`,
              onClick: () => {
                handleSubmit().then(data => {
                  if (data === true) {
                    clearData();
                    history.go(-1);
                  } else {
                    return false;
                  }
                });
              },
              type: "primary"
            }
          ]
        : [
            {
              // key: "submit",
              // text: `${getLabel("16634", "确认")}`,
              // onClick: () => history.go(-1),
              // className: "am-button-primary"
            }
          ];
      return (
        <div className="g-hocRouterLayout">
          <RouteLayout>
            <div
              key="wmPoutePageDemo"
              //className="wm-route-layout wm-search-advanced-modal"
              className="wm-route-layout"
            >
              <WrappedComponent {...this.props} />
              {hasBtns && <ButtonGroup data={btns} isButton={true} />}
            </div>
          </RouteLayout>
        </div>
      );
    }
  };
}
export default HocRouteLayout;
